package org.ainlolcat.ainscience2.controller.loader;

import org.jdom.Element;

import java.util.ArrayList;
import java.util.Collection;

/**
 * Created by IntelliJ IDEA.
 * User: ain
 * Date: 17.06.12
 * Time: 1:44
 * To change this template use File | Settings | File Templates.
 */
public class LoaderDefinition {
    // define if loader must be created or just exist as definition
    boolean instantiable = false;
    String name;
    String namespace;
    String clazz;
    Element parameters;
    LoaderDefinition parent;

    public LoaderDefinition(boolean instantiable, String name, String namespace, String clazz, Element parameters) {
        this.instantiable = instantiable;
        this.name = name;
        this.namespace = namespace;
        this.clazz = clazz;
        this.parameters = parameters;
    }

    public LoaderDefinition(boolean instantiable, String name, String namespace, String clazz, Element parameters, LoaderDefinition parent) {
        this.instantiable = instantiable;
        this.name = name;
        this.namespace = namespace;
        this.clazz = clazz;
        this.parameters = parameters;
        this.parent = parent;
    }

    public boolean isInstantiable() {
        return instantiable;
    }

    public String getName() {
        return name;
    }

    public String getNamespace() {
        return namespace;
    }

    public String getClazz() {
        return clazz;
    }

    public Element getParameters() {
        return parameters;
    }

    public LoaderDefinition getParent(){
        return parent;
    }
    
    public LoaderDefinition createChild(LoaderDefinition definition){
        Element source = (Element) parameters.clone();
        source.detach();
        Collection<Element> children = definition.getParameters().getChildren();
        for (Element e:children){
            Element temp = (Element) e.clone();
            temp.detach();
            source.addContent(temp);
        }
        
        return new LoaderDefinition(
                definition.isInstantiable(),
                definition.getName(),
                definition.getNamespace(),
                definition.getClazz(),
                source
        );
    }
}
